<template>
    <orh-trees add="add" @on-add="moralAdd" reload @on-reload="reload" del="del" @on-del="delConfirm">

        <Tree :data="trees" slot="trees" :load-data="childChange" @on-select-change="childValue"></Tree>

        <orh-sub-table pages :pageDate="pageDate" @on-page-change="reload" slot="content">
            <Table border stripe :data="data" :columns="columns" highlight-row @on-selection-change="selectOne" :loading="tableload" slot="table"></Table>
        </orh-sub-table>

        <orh-sidebar v-model="show" :title="titles" scale slot="others">
            <component :is="content" :condition="condition" @requery="requery"></component>
        </orh-sidebar>

    </orh-trees>
</template>
                
<script>

export default {
    name: 'dutymodule-admininspectitem',
    components: {
        Operation: resolve => {require(['./Operation.vue'], resolve);},
    },
    data () {
        return {
            show: false,
            content: null,
            titles: null,
            tableload: false,
            pageDate: {
                total: 0,
                current_page: 1,
                per_page: 0
            },
            treeid: null,
            trees: [],
            data:[],
            columns:[
                {
                    type: 'selection',
                    width: 60,
                    align: 'center'
                },
                {
                    title: '行政值班检查项名称',
                    minWidth: 150,
                    align: 'center',
                    render: (h, params) => {
                        let name = params.row.administrativeduty_name.zh? params.row.administrativeduty_name.zh: params.row.administrativeduty_name.mn;
                        return h('div',name)
                    }
                },
                {
                    title: '操作',
                    key: 'action',
                    minWidth: 150,
                    align: 'center',
                    render: (h, params) => {
                        return h('div', [
                            h('Button', {
                                directives: [
                                    {
                                        name: 'button',
                                        value: 'edit',
                                    }
                                ],
                                props: {
                                    type: 'primary',
                                    size: 'small',
                                },
                                style: {
                                    marginRight: '5px',
                                },
                                on: {
                                    click: () => {
                                        this.updata(params.row.admininspectterm_id)
                                    },
                                },
                            }, '修改'),
                            h('Poptip', {
                                props: {
                                    confirm: true,
                                    transfer: true,
                                    title: '您确定要删除这条数据吗?',
                                    type: 'error',
                                    size: 'small',
                                    placement:'left-end'
                                },
                                style: {
                                    textAlign: 'left',
                                },
                                on: {
                                    'on-ok': () => {
                                        this.remove(params.row.admininspectterm_id)
                                    }
                                }
                            }, [
                                h('Button', {
                                    directives: [
                                        {
                                            name: 'button',
                                            value: 'del',
                                        }
                                    ],
                                    props: {
                                        type: 'error',
                                        size: 'small'
                                    }
                                }, '删除')
                            ]),
                        ]);
                    }
                },
            ],
            url: 'api/watchlist/v1/get/admininspectterm/listss',
            delUrl: 'api/watchlist/v1/admininspectterm/del/',
            del: [],
            condition: null,
            url1: 'api/watchlist/v1/admininspectterm/list?administrativeduty_id=',
            url2: 'api/watchlist/v1/get/admininspectterm/listss?administrativeduty_id=',

            url3: 'api/watchlist/v1/get/admininspectterm/listspids?admininspectterm_id=',
            url4: 'api/watchlist/v1/get/admininspectterm/listspid?admininspectterm_id=',
            adminUrl: 'api/watchlist/v1/get/administrativeduty/index',
        }
    },

    methods: {
        
        /************************************************************
         * 
         * 删除确认
         * 
         ************************************************************/
        delConfirm () {
            if (this.del.length) {
                this.$Modal.confirm({
                    title: '删除确认',
                    closable: true,
                    loading: true,
                    content: '您确定要删除这些数据吗?',
                    onOk: () => {
                        this.delSubmit ()
                    }
                })
            }
        },
        
        /************************************************************
         * 
         * 添加数据
         * 
         ************************************************************/
        moralAdd () {
            this.condition = false
            this.titles = '添加数据'
            this.show = true
            this.content = 'Operation'
        },
        
        /************************************************************
         * 
         * 单条数据删除
         * @param   {index}            当前数据id
         * 
         ************************************************************/
        remove (index) {
            this.del = []
            this.del.push(index)
            this.delSubmit()
        },
        
        /************************************************************
         * 
         * 修改数据
         * @param   {index}            当前数据id
         * 
         ************************************************************/
        updata (index) {
            this.condition = index
            this.titles = '修改数据'
            this.show = true
            this.content = 'Operation'
        },
        
        /************************************************************
         * 
         * 批量删除数据
         * @param   {index}            所选数据id[array]
         * 
         ************************************************************/
        selectOne (index) {
            this.del = []
            index.forEach(item => {
                this.del.push(item.admininspectterm_id)
            });
        },
        
        /************************************************************
         * 
         * 删除操作
         * 
         ************************************************************/
        delSubmit () {
            if(this.del.length == 0){
                this.$Message.info('请选择删除项')
                this.prompt = false
                return
            }
            this.del = JSON.stringify(this.del)
            this.$http.delete(this.delUrl + this.del).then(val=>{
                this.$Modal.remove ()
                this.reload (this.pageDate.current_page)
            }).catch(err => {
                this.$Modal.remove ()
            })
        },
        
        /************************************************************
         * 
         * 添加，修改数据成功后关闭组件并刷新数据
         * @param   {state}            状态
         * 
         ************************************************************/
        requery (state) {
            if(state){
                this.show = false
                this.reload (this.pageDate.current_page)
            }
        },
        
        /************************************************************
         * 
         * 刷新数据
         * @param   {index}            分页
         * 
         ************************************************************/
        reload (index) {
            this.data = []
            this.tableload = true
            this.queryData(index?index:1)
        },
        
        /************************************************************
         * 
         * 列表数据
         * @param   {index}             分页
         * 
         ************************************************************/
        queryData (index) {
            this.tableload = true;
            let params = {
                page: index
            }
            this.$http.get(this.url, { params }).then(val=>{
                this.pageDate.per_page = val.pages.per_page
                this.pageDate.total = val.pages.total
                this.pageDate.current_page = val.pages.current_page
                this.data = val.data 
                this.tableload = false
            }).catch(err => {
                this.tableload = false
            })
        },
        
        // ==================================================
        // 点击行政值班项
        // ==================================================
        childValue(index){
            if(index.length != 0){
                this.tableload = true
                this.adminid = index[0].value
                if(index[0].state == 1){
                    this.url = this.url2+this.adminid
                    this.queryData(1)
                }
                if(index[0].state == 2){
                    this.url = this.url4+this.adminid
                    this.queryData(1)
                }
            }
        },

        // ==================================================
        // 获取行政值班项的子级
        // ==================================================
        childChange(index){
            if(index.state == 1){
                this.childData(index)
            }
            if(index.state == 2){
                this.childsData(index)
            }
        },

        // ==================================================
        // 查询行政值班项数据
        // ==================================================
        childData(index){
            this.$http.get(this.url1 + index.value).then(val=>{
                let datas = JSON.parse(JSON.stringify(val.data))
                datas.forEach(item => {
                    index.children.push({
                        title : item.administrativeduty_name.zh,
                        value : item.admininspectterm_id,
                        expand : false,
                        loading : false,
                        state : 2,
                        children : [],
                    })
                })
                index.expand = true
                index.loading = false
            }).catch(err=>{
                if(err.code && err.code==422) {
                    this.$Message.error(err.message);
                }
            })
        },

        // ==================================================
        // 查询检查项子级
        // ==================================================
        childsData(index){
            this.$http.get(this.url3 + index.value).then(val=>{
                let datas = JSON.parse(JSON.stringify(val.data))
                if(datas.length == 0){
                    delete index.expand
                }else{
                    datas.forEach(item => {
                        index.children.push({
                            title : item.administrativeduty_name.zh,
                            value : item.admininspectterm_id,
                            expand : false,
                            loading : false,
                            state : 2,
                            children : [],
                        })
                    })
                    index.expand = true
                }
                index.loading = false
            }).catch(err=>{
                if(err.code && err.code==422) {
                    this.$Message.error(err.message);
                }
            })
        },

        // ==================================================
        // 查询行政值班项数据
        // ==================================================
        adminData(){
            this.$http.get(this.adminUrl).then(val=>{
                let datas = JSON.parse(JSON.stringify(val.data))
                datas.forEach(this.handleAdmin)
                this.tableload = true
                this.url = this.url2
                this.queryData(this.pageDate.current_page)
            }).catch(err=>{
                if(err.code && err.code==422) {
                    this.$Message.error(err.message);
                }
            })
        },

        // ==================================================
        // 行政值班项数据处理
        // ==================================================
        handleAdmin(item,index){
            if(index == 0){
                this.adminid = item.administrativeduty_id
            }
            this.trees.push({
                title : item.administrativeduty_name.zh,
                value : item.administrativeduty_id,
                expand : false,
                loading : false,
                state : 1,
                children : [],
            })
        },
    },
    created () {
        this.adminData ()
    },
}
</script>